********************************************************************************
*                             Analysis of Article 5 Experiment
********************************************************************************

* Code by Paul Musgrave (musgrave@umass.edu)
* For Paul Musgrave and Lindsey Guenther
* 2022-January-03 Revisions

* README: This code takes the cleaned data from the NATO Cyber Experiments
* conducted in April 2021 and turns them into a series of tables and charts
* for use in an appendix and manuscript.

********************************************************************************
*                                   Stata Stuff                                *
********************************************************************************

clear all

set more off

set scheme plotplainblind

global MyDocs "/Users/paulmusgrave/Dropbox/0001 Academic Projects/Ongoing/"
global MyProject "${MyDocs}0151 Lindsey NATO Cyber/Posted/Dataverse"

use "${MyProject}/NATOCyberCleaned.dta"


drop  afghan* startdate enddate progress durationinseconds recordeddate ///
 yearborn gender raceaian raceasian raceblack racepacificislander ///
racewhite hispanic education partyidmain partyidstrengthd partyidstrengthr ///
partyidlean veteran veteranbranch internetfamiliarityusingalaptopc ///
internetfamiliaritycreatingandma internetfamiliaritywritingcodein ///
internetfamiliarityusingsocialne internetfamiliarityusingofficeso ///
internetfamiliaritymanagingfiles internettrickspasswords ///
internettrickstwofactor internettrickslastpass internettricksnone ///
intlattitudestheusshouldminditso intlattitudestheusshouldsupporti ///
intlattitudestheushasaspecialrol intlprioritiesmaintainingsuperio ///
intlprioritiesstrengtheningtheun intlprioritiescombatingworldhung ///
intlprioritiesimprovingtheglobal intlprioritiesdefendingusallies ///
knowledgescanning knowledgestable knowledgeuae knowledgelula ///
natobackgroundcheck knowledgecoup knowledgenukes knowledgespace ///
knowledgebrazil ifweb ifcode ifsocial ifoffice iffiles ///
attentivenessbrowser attentivenessmcu attentivenessnato attitudesownway ///
attitudesisrael attitudesexceptional prioritymilitary  ///
 art5911 art5estonia art5deaths

lab var blockorder "Order of Experiments"

order art5treatment art5fatalities art5supportDV, after(responseid)



********************************************************************************
*                       Organizing Code / Macros                               *
********************************************************************************

local demographics			     age white female hispanicnum college veteran i.pid7
local technical				       attentiveness blockorder
local internetfamiliarity 	 ifdim1 i.internethours2
local cyberthreat		         internettricks internetvictim ipnumeric cyberworry2

********************************************************************************
*                      			 Core Analysis        	                       *
********************************************************************************

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*					Analysis of Article 5 Support DV							  *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

quietly ologit 	art5supportDV i.art5treatment i.art5fatalities ///
				`demographics' `technical'
contrast r.art5treatment

* Base model
quietly ologit 	art5supportDV i.art5treatment i.art5fatalities
eststo 	h1m1

* Base with controls
quietly ologit 	art5supportDV i.art5treatment i.art5fatalities ///
				`demographics' `technical'
eststo 	h1m2

* Base, controls, and Internet familiarity
quietly ologit 	art5supportDV i.art5treatment i.art5fatalities ///
				`demographics' `technical' `internetfamiliarity'
eststo 	h1m3

* Base, controls, and militant
quietly ologit 	art5supportDV i.art5treatment i.art5fatalities ///
				`demographics' `technical' ///
				 militantinternationalism cooperativeinternationalism
eststo 	h1m4


* Base, controls, and cyberthreat
quietly ologit 	art5supportDV i.art5treatment i.art5fatalities ///
				`demographics' `technical' ///
				 `cyberthreat'
eststo 	h1m5


* Base, controls, and knowledge
quietly ologit 	art5supportDV i.art5treatment i.art5fatalities ///
				`demographics' `technical' ///
				 knowledgeindex
eststo 	h1m6


* Base with controls
quietly ologit 	art5supportDV i.art5treatment i.art5fatalities ///
				`demographics' `technical' if attentiveness == 3
eststo 	h1m7

************************ Presenting Results ************************************

* * * * * * * * * * * *  Tables  * * * * * * * * * * * * * * * * * * * * * * * *



esttab 	h1m1 h1m2 h1m3 h1m4 h1m5 h1m6, ///
				lab noomitted nobase varwidth(25) nodepvars compress ///
				mtitles("Base" "Controls" "Internet" "MI/CI" "Threat" "Knowledge") ///
        eqlabel(none)

esttab 	h1m1 h1m2 h1m3 h1m4 h1m5 h1m6 using "${MyProject}/Article5TableH1.tex", ///
				lab noomitted nobase varwidth(25) nodepvars compress replace ///
				mtitles("Base" "Controls" "Internet" "MI/CI" "Threat" "Knowledge") ///
        title("Precedent Experiment Testing Hypotheses 1 and 2, Main DV" \label{tab:art5nh1}) ///
        eqlabel(none) longtable notes ///
        refcat( 1.internethours2 "Daily Internet Use (Hours)" ///
                1.art5treatment "Article 5 Treatment" ///
                1.art5fatalities "Number of Fatalities" ///
                age "Demographics" ///
                , nolabel)


esttab 	h1m1 h1m2 h1m3 h1m4 h1m5 h1m6 using "${MyProject}/Article5TableH1.rtf", ///
      	lab noomitted nobase varwidth(25) nodepvars compress replace ///
        mtitles("Base" "Controls" "Internet" "MI/CI" "Threat" "Knowledge") ///
        title("Precedent Experiment Testing Hypotheses 1 and 2, Main DV") ///
        eqlabel(none) notes ///
        refcat( 1.internethours2 "Daily Internet Use (Hours)" ///
                1.art5treatment "Article 5 Treatment" ///
                1.art5fatalities "Number of Fatalities" ///
                age "Demographics" ///
                , nolabel)

* * * * * * * * * * * *  Graphs  * * * * * * * * * * * * * * * * * * * * * * * *

* Coefficient plot



coefplot  (h1m1, label(Base, No Controls) offset(.3)) ///
          (h1m2, label(Base With Controls) offset(.2)) ///
          (h1m3, label(Internet Knowledge) offset(.1)) ///
          (h1m4, label(Internationalism)) ///
          (h1m5, label(Internet Threat) offset(-0.1)) ///
          (h1m6, label(Political Knowledge) offset(-0.2)) ///
		  (h1m7, label(Attentive Only) offset(-0.3)), drop(_cons) ///
          keep(*.art5treatment *.art5fatalities) ///
          noomitted nobase xline(0) ///
          title({bf: A: Coefficient Estimates for Treatments}, size(large) span) ///
          subtitle({it:Ordinal logistic coefficients shown}, ///
            size(small) span) ///
          caption("N of 1187 for all models except for Knowledge, which has 1186, and Attentive Only, which has 806." ///
            , size(vsmall)) ///
          headings(1.art5treatment = "{bf:Article 5 Treatment}" ///
            1.art5fatalities = "{bf:Number of Fatalities}") ///
            name(coefficients,replace) legend(rows(3) ring(0) pos(12))

graph export "${MyProject}/Art5ResultsH1.png", replace


* Five value variant
mylabels 0(10)50, myscale(@/100) local(myla)
quietly ologit 	art5supportDV i.art5treatment i.art5fatalities ///
				`demographics' `technical'
margins     art5fatalities, atmeans
marginsplot, title({bf: B: Predicted Outcomes for Article 5}, size(large) span) ///
             subtitle({it:By Fatalities; Other Variables at Means}) ///
             plotdim(,elabels(1 "Strongly Oppose" 2 "Oppose" 3 "Neither" ///
                              4 "Approve" 5 "Strongly Approve")) ///
             ylab(`myla') name(predicted,replace) legend(rows(1) pos(12))

graph export "${MyProject}/Art5ResultsH1Predicted.png", replace
margins      art5fatalities, atmeans contrast(effects)

gr combine  coefficients predicted, rows(2) ysize(10)
gr export   "${MyProject}/Art5ResultsCoefficientsPredicted.png", replace
gr export   "${MyProject}/Art5ResultsCoefficientsPredicted.pdf", replace
gr export   "${MyProject}/Art5ResultsCoefficientsPredicted.eps", replace 


* Bar chart variant
quietly ologit 	art5supportDV i.art5treatment i.art5fatalities ///
				`demographics' `technical'
margins, atmeans at(art5fatalities==0) post
eststo artdeaths0

quietly ologit 	art5supportDV i.art5treatment i.art5fatalities ///
				`demographics' `technical'
margins, atmeans at(art5fatalities==1) post
eststo artdeaths1

coefplot artdeaths0 artdeaths1, ///
title({bf: B: Predicted Support for Invoking Article 5}, size(large) span) ///
             subtitle({it:By Fatalities; Other Variables at Means}) ///
	recast(bar) barw(0.15) ///
	ciopts(recast(rcap) color(gs8)) citop ///
	 ylab(1 "Strongly Oppose" 2 "Oppose" 3 "Neither" 4"Approve" ///
					5 "Strongly Approve") ///
	legend(order(1 "No deaths" 3 "20 deaths") ring(0) pos(2)) ///
	xtitle("Predicted Probability of Choosing Each Response") ///
	name(art5fatalitiesbar, replace) xlab(`myla') 
 
 
  
gr combine  coefficients art5fatalitiesbar, rows(2) ysize(10)
gr export   "${MyProject}/Art5ResultsCoefficientsPredictedBar.png", replace
gr export   "${MyProject}/Art5ResultsCoefficientsPredictedBar.pdf", replace
gr export   "${MyProject}/Art5ResultsCoefficientsPredictedBar.eps", replace


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*					Analysis of Article 5 Do Nothing DV
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* Base model
quietly reg 	art5specificdonothing i.art5treatment i.art5fatalities
eststo 	h1altm1

* Base with controls
quietly reg 	art5specificdonothing i.art5treatment i.art5fatalities ///
				`demographics' `technical'
eststo 	h1altm2

* Base, controls, and Internet familiarity
quietly reg 	art5specificdonothing i.art5treatment i.art5fatalities ///
				`demographics' `technical' `internetfamiliarity'
eststo 	h1altm3

* Base, controls, and militant
quietly reg 	art5specificdonothing i.art5treatment i.art5fatalities ///
				`demographics' `technical' ///
				 militantinternationalism cooperativeinternationalism
eststo 	h1altm4


* Base, controls, and cyberthreat
quietly reg 	art5specificdonothing i.art5treatment i.art5fatalities ///
				`demographics' `technical' ///
				 `cyberthreat'
eststo 	h1altm5


* Base, controls, and knowledge
quietly reg 	art5specificdonothing i.art5treatment i.art5fatalities ///
				`demographics' `technical' ///
				 knowledgeindex
eststo 	h1altm6




esttab 	h1altm1 h1altm2 h1altm3 h1altm4 h1altm5 h1altm6, ///
				lab noomitted nobase varwidth(25) nodepvars compress ///
				mtitles("Base" "Controls" "Internet" "MI/CI" "Threat" "Knowledge") ///
        eqlabel(none)

esttab 	h1altm1 h1altm2 h1altm3 h1altm4 h1altm5 h1altm6 using "${MyProject}/Article5Tableh1alt.tex", ///
				lab noomitted nobase varwidth(25) nodepvars compress replace ///
				mtitles("Base" "Controls" "Internet" "MI/CI" "Threat" "Knowledge") ///
        title("Precedent Experiment Testing Hypotheses 1 and 2, Alternate DV" \label{tab:art5nh1alt}) ///
        eqlabel(none) longtable notes ///
        refcat( 1.internethours2 "Daily Internet Use (Hours)" ///
                1.art5treatment "Article 5 Treatment" ///
                1.art5fatalities "Number of Fatalities" ///
                age "Demographics" ///
                , nolabel)


esttab 	h1altm1 h1altm2 h1altm3 h1altm4 h1altm5 h1altm6 using "${MyProject}/Article5Tableh1alt.rtf", ///
      	lab noomitted nobase varwidth(25) nodepvars compress replace ///
        mtitles("Base" "Controls" "Internet" "MI/CI" "Threat" "Knowledge") ///
        title("Precedent Experiment Testing Hypotheses 1 and 2, Alternate DV") ///
        eqlabel(none) notes ///
        refcat( 1.internethours2 "Daily Internet Use (Hours)" ///
                1.art5treatment "Article 5 Treatment" ///
                1.art5fatalities "Number of Fatalities" ///
                age "Demographics" ///
                , nolabel)


********************************************************************************
*                      			 Interaction Analysis        	                       *
********************************************************************************

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*					Analysis of Article 5 Support DV							  *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* Base model
quietly ologit 	art5supportDV i.art5treatment##i.art5fatalities
eststo 	h3m1

* Base with controls
quietly ologit 	art5supportDV i.art5treatment##i.art5fatalities ///
				`demographics' `technical'
eststo 	h3m2

* Base, controls, and Internet familiarity
quietly ologit 	art5supportDV i.art5treatment##i.art5fatalities ///
				`demographics' `technical' `internetfamiliarity'
eststo 	h3m3

* Base, controls, and militant
quietly ologit 	art5supportDV i.art5treatment##i.art5fatalities ///
				`demographics' `technical' ///
				 militantinternationalism cooperativeinternationalism
eststo 	h3m4


* Base, controls, and cyberthreat
quietly ologit 	art5supportDV i.art5treatment##i.art5fatalities ///
				`demographics' `technical' ///
				 `cyberthreat'
eststo 	h3m5


* Base, controls, and knowledge
quietly ologit 	art5supportDV i.art5treatment##i.art5fatalities ///
				`demographics' `technical' ///
				 knowledgeindex
eststo 	h3m6



esttab 	h3m1 h3m2 h3m3 h3m4 h3m5 h3m6, ///
				lab noomitted nobase varwidth(25) nodepvars compress ///
				mtitles("Base" "Controls" "Internet" "MI/CI" "Threat" "Knowledge") ///
        eqlabel(none)

esttab 	h3m1 h3m2 h3m3 h3m4 h3m5 h3m6 using "${MyProject}/Article5Tableh3.tex", ///
				lab noomitted nobase varwidth(25) nodepvars compress replace ///
				mtitles("Base" "Controls" "Internet" "MI/CI" "Threat" "Knowledge") ///
        title("Precedent Experiment Testing Hypothesis 3, Main DV" \label{tab:art5nh3}) ///
        eqlabel(none) longtable notes ///
        refcat( 1.internethours2 "Daily Internet Use (Hours)" ///
                1.art5treatment "Article 5 Treatment" ///
                1.art5fatalities "Number of Fatalities" ///
                age "Demographics" ///
                , nolabel)


esttab 	h3m1 h3m2 h3m3 h3m4 h3m5 h3m6 using "${MyProject}/Article5Tableh3.rtf", ///
      	lab noomitted nobase varwidth(25) nodepvars compress replace ///
        mtitles("Base" "Controls" "Internet" "MI/CI" "Threat" "Knowledge") ///
        title("Precedent Experiment Testing Hypothesis 3, Main DV") ///
        eqlabel(none) notes ///
        refcat( 1.internethours2 "Daily Internet Use (Hours)" ///
                1.art5treatment "Article 5 Treatment" ///
                1.art5fatalities "Number of Fatalities" ///
                age "Demographics" ///
                , nolabel)


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*					Analysis of Afghan Support DV
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* Base model
quietly reg 	art5specificdonothing i.art5treatment##i.art5fatalities
eststo 	h3altm1

* Base with controls
quietly reg 	art5specificdonothing i.art5treatment##i.art5fatalities ///
				`demographics' `technical'
eststo 	h3altm2

* Base, controls, and Internet familiarity
quietly reg 	art5specificdonothing i.art5treatment##i.art5fatalities ///
				`demographics' `technical' `internetfamiliarity'
eststo 	h3altm3

* Base, controls, and militant
quietly reg 	art5specificdonothing i.art5treatment##i.art5fatalities ///
				`demographics' `technical' ///
				 militantinternationalism cooperativeinternationalism
eststo 	h3altm4


* Base, controls, and cyberthreat
quietly reg 	art5specificdonothing i.art5treatment##i.art5fatalities ///
				`demographics' `technical' ///
				 `cyberthreat'
eststo 	h3altm5


* Base, controls, and knowledge
quietly reg 	art5specificdonothing i.art5treatment##i.art5fatalities ///
				`demographics' `technical' ///
				 knowledgeindex
eststo 	h3altm6


esttab 	h3altm1 h3altm2 h3altm3 h3altm4 h3altm5 h3altm6, ///
				lab noomitted nobase varwidth(25) nodepvars compress ///
				mtitles("Base" "Controls" "Internet" "MI/CI" "Threat" "Knowledge") ///
        eqlabel(none)

esttab 	h3altm1 h3altm2 h3altm3 h3altm4 h3altm5 h3altm6 using "${MyProject}/Article5Tableh3alt.tex", ///
				lab noomitted nobase varwidth(25) nodepvars compress replace ///
				mtitles("Base" "Controls" "Internet" "MI/CI" "Threat" "Knowledge") ///
        title("Precedent Experiment Testing Hypothesis 3, Alternate DV" \label{tab:art5nh3alt}) ///
        eqlabel(none) longtable notes ///
        refcat( 1.internethours2 "Daily Internet Use (Hours)" ///
                1.art5treatment "Article 5 Treatment" ///
                1.art5fatalities "Number of Fatalities" ///
                age "Demographics" ///
                , nolabel)


esttab 	h3altm1 h3altm2 h3altm3 h3altm4 h3altm5 h3altm6 using "${MyProject}/Article5Tableh3alt.rtf", ///
      	lab noomitted nobase varwidth(25) nodepvars compress replace ///
        mtitles("Base" "Controls" "Internet" "MI/CI" "Threat" "Knowledge") ///
        title("Precedent Experiment Testing Hypothesis 3, Alternate DV") ///
        eqlabel(none) notes ///
        refcat( 1.internethours2 "Daily Internet Use (Hours)" ///
                1.art5treatment "Article 5 Treatment" ///
                1.art5fatalities "Number of Fatalities" ///
                age "Demographics" ///
                , nolabel)



********************************************************************************
*                      			 Subsidiary Analysis       	                       *
********************************************************************************

* Here, we present analyses of each specific strategy, but because they are all
* null we present them as tables only.

* First, we have to recast the data so that it is in long format and then
* work to get all the variables pretty


reshape long art5specific, j(strategy) i(responseid) string
replace strategy = "Do nothing" if strategy == "donothing"
replace strategy = proper(strategy)


lab def strategylab 0 "Do Nothing" 1 "Treaty" 2 "Sanctions" 3 "Indict" ///
                    4 "Cyberattack" 5 "Airstrikes"
encode strategy, gen(strategycode) lab(strategylab)
lab var strategycode "Strategy Type"


quietly eststo h4m1: reg art5specific i.art5treatment i.art5fatalities ///
                  i.strategycode  ///
                  if strategycode != 0

quietly eststo h4m2: reg art5specific i.art5treatment i.art5fatalities ///
                  i.strategycode  ///
                  `demographics' `technical' if strategycode != 0


quietly eststo h4m3: reg art5specific i.art5treatment i.art5fatalities ///
                  i.strategycode  ///
                  `demographics' `technical' `internetfamiliarity' if strategycode != 0

quietly eststo h4m4: reg art5specific i.art5treatment i.art5fatalities ///
                  i.strategycode  ///
                  `demographics' `technical' ///
          				 militantinternationalism cooperativeinternationalism if strategycode != 0

quietly eststo h4m5: reg art5specific i.art5treatment i.art5fatalities ///
                 i.strategycode  ///
                 `demographics' `technical'  `cyberthreat' if strategycode != 0


quietly eststo h4m6: reg art5specific i.art5treatment i.art5fatalities ///
                i.strategycode  ///
                `demographics' `technical' knowledgeindex if strategycode != 0


esttab 	h4m1 h4m2 h4m3 h4m4 h4m5 h4m6 using "${MyProject}/Art5SpecificResults.tex", ///
				lab noomitted nobase varwidth(25) nodepvars compress replace ///
				mtitles("Base" "Controls" "Internet" "MI/CI" "Threat" "Knowledge") ///
        title("Afghan Experiment Testing Hypothesis 4, Specific Strategies" \label{tab:afghanh4}) ///
        eqlabel(none) longtable notes ///
        refcat( 1.internethours2 "Daily Internet Use (Hours)" ///
                1.art5treatment "Russia Attribution" ///
                1.art5fatalities "Russian Reaction" ///
                age "Demographics" ///
                2.strategycode "Strategy Types" ///
                , nolabel)


esttab 	h4m1 h4m2 h4m3 h4m4 h4m5 h4m6 using "${MyProject}/Art5SpecificResults.rtf", ///
      	lab noomitted nobase varwidth(25) nodepvars compress replace ///
        mtitles("Base" "Controls" "Internet" "MI/CI" "Threat" "Knowledge") ///
        title("Afghan Experiment Testing Hypothesis 4, Specific Strategies") ///
        eqlabel(none) notes ///
        refcat( 1.internethours2 "Daily Internet Use (Hours)" ///
                1.art5treatment "Russia Attribution" ///
                1.art5fatalities "Russian Reaction" ///
                age "Demographics" ///
                2.strategycode "Strategy Types" ///
                , nolabel)
